using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

using SharpCore.Data;
using SharpCore.Utilities;

namespace Dating.DA
{
	public class AlertDA
	{
		#region Fields

		protected string connectionStringName;

		#endregion

		#region Constructors
		public AlertDA()
		{
		}

		public AlertDA(string connectionStringName)
		{
			ValidationUtility.ValidateArgument("connectionStringName", connectionStringName);

			this.connectionStringName = connectionStringName;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the Alerts table.
		/// </summary>
		public  int Insert(Alert alert)
		{
			ValidationUtility.ValidateArgument("alert", alert);

			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@TenantID", alert.TenantID),
				new SqlParameter("@AlertMessage", alert.AlertMessage)
			};

			alert.AlertID = Convert.ToInt32(SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "AlertsInsert", parameters));
			return alert.AlertID;
		}

		/// <summary>
		/// Updates a record in the Alerts table.
		/// </summary>
		public  int Update(Alert alert)
		{
			ValidationUtility.ValidateArgument("alert", alert);

			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@AlertID", alert.AlertID),
				new SqlParameter("@TenantID", alert.TenantID),
				new SqlParameter("@AlertMessage", alert.AlertMessage)
			};

			return(SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "AlertsUpdate", parameters));
		}

		/// <summary>
		/// Deletes a record from the Alerts table by its primary key.
		/// </summary>
		public  int Delete(int alertID, int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@AlertID", alertID)
,				new SqlParameter("@TenantID", tenantID)
			};

			return(SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "AlertsDelete", parameters));
		}

		/// <summary>
		/// Deletes a record from the Alerts table by a foreign key.
		/// </summary>
		public  int DeleteAllByAlertID(int alertID, int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@AlertID", alertID)
,				new SqlParameter("@TenantID", tenantID)
			};

			return(SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "AlertsDeleteAllByAlertID", parameters));
		}

		/// <summary>
		/// Deletes a record from the Alerts table by a foreign key.
		/// </summary>
		public  int DeleteAllByTenantID(int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@TenantID", tenantID)

			};

			return(SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "AlertsDeleteAllByTenantID", parameters));
		}

		/// <summary>
		/// Selects a single record from the Alerts table.
		/// </summary>
		public  Alert Select(int alertID, int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@AlertID", alertID)
,				new SqlParameter("@TenantID", tenantID)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "AlertsSelect", parameters))
			{
				if (dataReader.Read())
				{
					return MakeAlert(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

		/// <summary>
		/// Selects all records from the Alerts table.
		/// </summary>
		public  List<Alert> SelectAll(int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@TenantID", tenantID)
			};
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "AlertsSelectAll", parameters))
			{
				List<Alert> alertList = new List<Alert>();
				while (dataReader.Read())
				{
					Alert alert = MakeAlert(dataReader);
					alertList.Add(alert);
				}

				return alertList;
			}
		}

		/// <summary>
		/// Selects all records from the Alerts table by a foreign key.
		/// </summary>
		public  List<Alert> SelectAllByAlertID(int alertID, int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@AlertID", alertID)
,				new SqlParameter("@TenantID", tenantID)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "AlertsSelectAllByAlertID", parameters))
			{
				List<Alert> alertList = new List<Alert>();
				while (dataReader.Read())
				{
					Alert alert = MakeAlert(dataReader);
					alertList.Add(alert);
				}

				return alertList;
			}
		}

		/// <summary>
		/// Selects all records from the Alerts table by a foreign key.
		/// </summary>
		public  List<Alert> SelectAllByTenantID(int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@TenantID", tenantID)

			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "AlertsSelectAllByTenantID", parameters))
			{
				List<Alert> alertList = new List<Alert>();
				while (dataReader.Read())
				{
					Alert alert = MakeAlert(dataReader);
					alertList.Add(alert);
				}

				return alertList;
			}
		}

		/// <summary>
		/// Creates a new instance of the Alerts class and populates it with data from the specified SqlDataReader.
		/// </summary>
		protected  Alert MakeAlert(SqlDataReader dataReader)
		{
			Alert alert = new Alert();
			alert.AlertID = SqlClientUtility.GetInt32(dataReader, "AlertID", 0);
			alert.TenantID = SqlClientUtility.GetInt32(dataReader, "TenantID", 0);
			alert.AlertMessage = SqlClientUtility.GetString(dataReader, "AlertMessage", String.Empty);

			return alert;
		}

		#endregion
	}
}
